//
//  ClubReportViewController.swift
//  77Club
//
//  Created by Apple on 2024/9/25.
//

import UIKit

class ClubReportViewController: ClubBaseViewController {

    private let closeButton = c_make_ex(UIButton.self)
        .image("Report_close").end()
    private let uploadButton = c_make_ex(UIButton.self)
        .image("Report_uploadSel").image("Report_uploadNor", state: .disabled).end()
    private let titleLabel = c_make_ex(UILabel.self)
        .text("举报动态").color(.black).font(16.font_semiBold).end()
    private let subLabel = c_make_ex(UILabel.self)
        .text("您认为被举报对象存在什么问题？").color(.black).font(12.font_regular).end()
    private var dataSource: [ClubReportModel] = [
        ClubReportModel(title: "垃圾营销", isSel: false),
        ClubReportModel(title: "淫秽色情", isSel: false),
        ClubReportModel(title: "不实消息", isSel: false),
        ClubReportModel(title: "人身攻击", isSel: false),
        ClubReportModel(title: "违法信息", isSel: false),
        ClubReportModel(title: "其他", isSel: false),
    ]
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.register(ClubReportCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.rowHeight = 70
        return tableView
    }()
    
    var updateDataBlock: (() -> Void)?
    
    var dynamicID = ""
    //0=社区新闻,1=动态,2=评论，3=帖子
    var type = "1"
    var isShequ = false
    
    init() {
        super.init(nibName: nil, bundle: nil)
        modalPresentationStyle = .fullScreen
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            closeButton
            uploadButton
            titleLabel
            subLabel
            tableView
        }
        
        closeButton.top(.statusHeight).leading(0).width(56.0.Scale).height(44.0.Scale)
        uploadButton.trailing(12.0.Scale).width(115.0.Scale).height(44.0.Scale)
        uploadButton.CenterY == closeButton.CenterY
        titleLabel.top(.navi_height+12.0).leading(16)
        subLabel.top(.navi_height+38.0).leading(16)
        tableView.leading(16).trailing(16).bottom(0)
        tableView.Top == subLabel.Bottom + 25
        
        uploadButton.isEnabled = false
        closeButton.addTarget(self, action: #selector(clickCloseButton), for: .touchUpInside)
        uploadButton.addTarget(self, action: #selector(clickUploadButton), for: .touchUpInside)
    }

    @objc private func clickCloseButton(){
        self.dismiss(animated: true)
    }
    
    @objc private func clickUploadButton(){
        //举报
        var remarkArray = [String]()
        for md in dataSource {
            if md.isSel {
                remarkArray.append(md.title)
            }
        }
        let remarks = remarkArray.joined(separator: ",")
        
        if isShequ {
            Task{
                
                let res = await network.requestSpecial(.shequReport(id: dynamicID, remark: remarks, type: self.type))
                if res.isSuccess {
                    self.updateDataBlock?()
                    self.dismiss(animated: true){
                        "举报成功".toast_success
                    }
                }
                
            }
        }else{
            Task{
                
                let res = await network.requestSpecial(.report(id: dynamicID, remark: remarks, type: self.type))
                if res.isSuccess {
                    self.updateDataBlock?()
                    self.dismiss(animated: true){
                        "举报成功".toast_success
                    }
                }
                
            }
        }
        
    }
    
    private func updateUploadButtonState(){
        
        var isSel = false
        for md in dataSource {
            if md.isSel {
                isSel = true
            }
        }
        uploadButton.isEnabled = isSel
    }
}

//MARK: - UITableViewDelegate, UITableViewDataSource
extension ClubReportViewController: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "ClubReportCell", for: indexPath) as! ClubReportCell
        cell.model = dataSource[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        dataSource[indexPath.row].isSel = !dataSource[indexPath.row].isSel

        updateUploadButtonState()
        tableView.reloadData()
    }

}


//MARK: 游戏攻略cell
class ClubReportCell: UITableViewCell {

    private let bgView = c_make_ex(UIView.self)
        .bgColor("#EDEEF0").corner(12.0.Scale).end()
    private let checkButton = c_make_ex(UIButton.self)
        .image("Report_checkNor").image("Report_checkSel", state: .selected).end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#47494D").font(15.font_medium).end()
    
    var model: ClubReportModel? {
        didSet{
            guard let md = model else { return }
            checkButton.isSelected = md.isSel
            if md.isSel {
                bgView.backgroundColor = "#B23636".toColor
                titleLabel.textColor = .white
            }else{
                bgView.backgroundColor = "#EDEEF0".toColor
                titleLabel.textColor = "#47494D".toColor
            }
            titleLabel.text = md.title
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            bgView.subviews{
                checkButton
                titleLabel
            }
        }

        checkButton.isUserInteractionEnabled = false
        
        bgView.leading(0).trailing(0).top(0).height(55)
        checkButton.leading(20).size(20).centerVertically()
        titleLabel.leading(52).centerVertically()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
